package com.blb.servlet.admin;


import com.alibaba.fastjson.JSONArray;
import com.blb.pojo.Carousel;
import com.blb.pojo.Page;
import com.blb.pojo.Result;
import com.blb.service.admin.ICarouselService;
import com.blb.service.admin.impl.CarouselServiceImpl;
import com.blb.servlet.BaseServlet;
import com.blb.utils.ApplicationContextUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(value = "/carousel/*", loadOnStartup = 1)
public class CarouselServlet extends BaseServlet {

    private ICarouselService carouselService = (ICarouselService) ApplicationContextUtil.getBean(ICarouselService.class);

    /**
     * 分页
     * @param req
     * @param resp
     */
    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int page = getPage();
        int size = getSize();

        try {
            Page<Carousel> page1 = carouselService.page(page, size);
            req.setAttribute("page", page1);
            forward("/admin/view/carousel/list");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    /**
     * 添加轮播图
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            Carousel carousel = getEntity(Carousel.class);
            carouselService.add(carousel);
            writeJson(Result.ok("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("添加失败："+e.getMessage()));
        }
    }

    /**
     * 删除
     * @param req
     * @param resp
     * @throws IOException
     */
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String id = req.getParameter("id");
            carouselService.deleteById(Integer.parseInt(id));
            writeJson(Result.ok("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("删除失败："+e.getMessage()));
        }
    }
    /**
     * 批量删除
     * @param req
     * @param resp
     * @throws IOException
     */
    public void batchDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            String ids = req.getParameter("ids");
            String[] idArr = JSONArray.parseArray(ids).toArray(new String[0]);
            carouselService.batchDelete(idArr);
            writeJson(Result.ok("批量删除成功"));

        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("批量删除失败"+e.getMessage()));
        }
    }


    /**
     * 跳转修改页面
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updatePage(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        try {
            Carousel carousel = carouselService.getById(Integer.parseInt(id));
            req.setAttribute("carousel",carousel);
            forward("/admin/view/carousel/update");
        } catch (Exception e) {
            e.printStackTrace();
            forward("/WEB-INF/jsp/500.jsp");
        }
    }

    /**
     * 修改轮播图
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        try {
            Carousel carousel = getEntity(Carousel.class);
            carouselService.update(carousel);
            writeJson(Result.ok("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            writeJson(Result.err("修改失败"+e.getMessage()));
        }
    }
}
